#include <fish.h>


Fish::Fish()
{

}

Fish::~Fish()
{

}

bool Fish::Init(const int & id,const int &type,CCLayer* pLayer)
{
	m_ID = id;
	m_Type = type;
	m_pLayer = pLayer;
	SetTargetPos();
	return Create();

}

bool Fish::Create()
{
	char* fileName[] = {"animations/dragon_animation.png","animations/dragon_animation-hd.png"};
	int size[] = {132,132*2};
	int nsize = size[m_Type];


	CCSize s = CCDirector::sharedDirector()->getWinSize();

	CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage(fileName[m_Type]);

	
	// manually add frames to the frame cache
	CCSpriteFrame *frame0 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(nsize*0, nsize*0, nsize, nsize));
	CCSpriteFrame *frame1 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(nsize*1, nsize*0, nsize, nsize));
	CCSpriteFrame *frame2 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(nsize*2, nsize*0, nsize, nsize));
	CCSpriteFrame *frame3 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(nsize*3, nsize*0, nsize, nsize));
	CCSpriteFrame *frame4 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(nsize*0, nsize*1, nsize, nsize));
	CCSpriteFrame *frame5 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(nsize*1, nsize*1, nsize, nsize));


	//
	// Animation using Sprite BatchNode
	//
	m_pSprite = CCSprite::spriteWithSpriteFrame(frame0);

	int x = abs(int((double)rand() / (RAND_MAX + 1) * s.width));
	int y = abs(int((double)rand() / (RAND_MAX + 1) * s.height));
	m_pSprite->setPosition( ccp( x,y) );
	m_pLayer->addChild(m_pSprite);

	CCMutableArray<CCSpriteFrame*> *animFrames = new CCMutableArray<CCSpriteFrame*>(6);
	animFrames->addObject(frame0);
	animFrames->addObject(frame1);
	animFrames->addObject(frame2);
	animFrames->addObject(frame3);
	animFrames->addObject(frame4);
	animFrames->addObject(frame5);

	CCAnimation *animation = CCAnimation::animationWithFrames(animFrames, 0.2f);
	CCAnimate *animate = CCAnimate::actionWithAnimation(animation, false);

	animate->reverse();

	/*CCActionInterval* seq = (CCActionInterval*)(CCSequence::actions( 
		animate,
		CCFlipX::actionWithFlipX(true),
		animate->copy()->autorelease(),
		CCFlipX::actionWithFlipX(false),
		NULL) );*/

	//CCActionInterval*  actionTo = CCRotateTo::actionWithDuration( 0, 0);
	CCFlipX * pFlip = CCFlipX::actionWithFlipX(true);
	m_ActionFlip =  CCSequence::actions(pFlip,NULL);
	m_pSprite->runAction(m_ActionFlip);
	m_pSprite->runAction(CCRepeatForever::actionWithAction( animate ) );
	animFrames->release();    // win32 : memory leak    2010-0415

	return true;
}

void Fish::SetTargetPos()
{
	CCSize s = CCDirector::sharedDirector()->getWinSize();

	m_TargetPos.x = abs(int((double)rand() / (RAND_MAX + 1) * (s.width+100))) -50;
	m_TargetPos.y = abs(int((double)rand() / (RAND_MAX + 1) * (s.height+100))) -50;


}

void Fish::Update(const ccTime &dt)
{
	float x ;
	float y ;

	CCPoint pos = m_pSprite->getPosition();
	x = m_TargetPos.x - pos.x;
	y = m_TargetPos.y - pos.y;

	float len = x*x + y*y;
	if(len < m_nSpeedLen)
	{
		


		m_pSprite->setPosition(m_TargetPos);
		SetTargetPos();
		if (m_TargetPos.x > m_pSprite->getPosition().x)
		{
			m_pSprite->stopAction(m_ActionFlip);
		}
		else
		{

			m_pSprite->stopAction(m_ActionFlip);

			bool b =  abs(int((double)rand() / (RAND_MAX + 1) * 2));
			CCFlipX * pFlip = CCFlipX::actionWithFlipX(b);
			CCFiniteTimeAction* tem =  CCSequence::actions(pFlip,NULL);
			m_pSprite->runAction(tem);
			//m_pSprite->runAction(m_ActionFlip);
		}
		
		return;
	}
	float rate = m_nSpeedLen/len;

	if(x >0)
	{
		pos.x += x*x*rate;
	}
	else
	{
		pos.x -= x*x*rate;
	}

	if(y >0)
	{
		pos.y +=  y*y*rate;
	}
	else
	{
		pos.y -= y*y*rate;
	}

	m_pSprite->setPosition( pos );


}

const bool& Fish::IsHit()
{
	int tem =  abs(int((double)rand() / (RAND_MAX + 1) * 100));
	if (tem < 50)
	{
		return true;
	}
	return false;
}

